/**
 * lodash 替换为全量引用，避免按需与全量混合使用引起lodash重复打包
 * @param {string} source
 */
 module.exports = function refactorLodash(source) {
  let newSource = source;

  // 1. import isNill from 'lodash/isNill' ==> import {isNill} from 'lodash'
  newSource = newSource.replace(/import\s+(.+?)\s+from\s+['"]lodash\/(.+?)['"]/g, (_, $1, $2) => {
    if ($1 === $2) return `import { ${$1} } from 'lodash'`;
  });
  // 2. require('lodash/slice')  ==> require('lodash').slice
  newSource = newSource.replace(/require\(['"]lodash\/(.+?)['"]\)/g, (_, $1) => {
    // lodash/fp/isNill
    if ($1.startsWith('fp/')) {
      // console.log(resourcePath)
      return `require('lodash/fp').${$1.split('/')[1]}`;
    }

    return `require('lodash').${$1}`;
  });
  return newSource;
}